package com.secneo.appshare.activities;

import java.util.List;

import android.app.ProgressDialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.secneo.appshare.AppShareApplication;
import com.secneo.appshare.R;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.RemoteApi.Medals;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.Constants;

public class MyZoneHonorActivity extends CommonMenuActivity implements
		OnClickListener {
	public String oneName = "", twoName = "";
	public boolean oneHasit, twoHasit;
	public static final int GETMEDALS = 2020;
	int honorNumber = 1;
	private ImageView badgeRookieImageView, badgeDownLoadImageView;
	private TextView honorNumTextView;
	private View mImgView = null;
	private PopupWindow mImgPop = null;
	private LinearLayout badgeMainLinearLayout;
	private RelativeLayout badgeBgLinearLayout;
	private ProgressDialog mProgressDialog = null;
	String mUserName;
	public Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {

			case GETMEDALS:
				if (mProgressDialog != null) {
					mProgressDialog.dismiss();
					mProgressDialog = null;
				}
				badgeMainLinearLayout.setVisibility(View.VISIBLE);
				if (twoHasit) {
					badgeDownLoadImageView.setEnabled(true);
					honorNumber++;
					honorNumTextView.setText(MyZoneHonorActivity.this
							.getResources().getString(
									R.string.member_setting_honor_gethonor));
				} else {
					honorNumTextView.setText(MyZoneHonorActivity.this
							.getResources().getString(
									R.string.member_setting_honor_not_gethonor,
									twoName));
				}
				honorNumTextView.setText(MyZoneHonorActivity.this
						.getResources().getString(
								R.string.member_setting_honor_number,
								honorNumber));
				break;
				
			case Constants.MSG_GET_RECOMS_NETWORK_ERROR:
				if (mProgressDialog != null)
				{
					mProgressDialog.dismiss();
					mProgressDialog = null;
				}
				View view = findViewById(R.id.view_network_error);
				view.setVisibility(View.VISIBLE);
				break;
			default:
				break;

			}
			super.handleMessage(msg);
		}
	};

	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.badge_main);
		AppShareApplication.getInstance().addActivity(this);
		LocalStore.getLocaleStoreInstance(this).setHonorCounts(0);
		Bundle bundle = getIntent().getExtras();

		mUserName = "";
		if (bundle.containsKey("username")) {
			mUserName = bundle.getString("username");
		}

		badgeMainLinearLayout = (LinearLayout) findViewById(R.id.BadgeMainLinearLayout);
		badgeRookieImageView = (ImageView) findViewById(R.id.BadgeRookieImageView);
		badgeDownLoadImageView = (ImageView) findViewById(R.id.BadgeDownLoadImageView);
		honorNumTextView = (TextView) findViewById(R.id.HonorNumTextView);
		badgeDownLoadImageView.setEnabled(false);
		// badgeDownLoadImageView.getBackground().setAlpha(100);
		badgeRookieImageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				initImagePopWindow(R.drawable.badge_rookie_pressed, "新手上路",
						"只要安装安卓分享并完成注册，即可获得本勋章");
			}
		});
		badgeDownLoadImageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				initImagePopWindow(R.drawable.badge_download_pressed, "下载狂",
						"三天内下载累计5个以上的应用则获得下载狂勋章");
			}
		});
		startProgressDialog();
		Thread honorThread = new Thread() {
			public void run() {
				String uId = LocalStore.getLocaleStoreInstance(
						MyZoneHonorActivity.this).getUid();
				RemoteApiImpl rai = new RemoteApiImpl();
				List<Medals> ret = rai.getMedals(MyZoneHonorActivity.this, uId);
				if (ret != null) {
					if (ret.size() == 2) {
						Medals m = ret.get(0);
						oneName = m.name;
						oneHasit = m.hasit;
						Medals m1 = ret.get(1);
						twoName = m1.name;
						twoHasit = m1.hasit;
						Log.d("MyTag", "twoHasit=" + twoHasit);
					}
					Message msg = new Message();
					msg.what = GETMEDALS;
					mHandler.sendMessage(msg);
					return;
				} else {
					Message msg = new Message();
					msg.what = Constants.MSG_GET_RECOMS_NETWORK_ERROR;
					mHandler.sendMessage(msg);
					return;
				}
			}
		};
		honorThread.start();

	}

	private void startProgressDialog() {
		View view = findViewById(R.id.view_network_error);
		view.setVisibility(View.GONE);
		badgeMainLinearLayout.setVisibility(View.GONE);
		if (mProgressDialog != null)
			mProgressDialog.dismiss();
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setMessage("加载中，请稍候...");
		mProgressDialog.setCancelable(false);
		mProgressDialog.show();
	}

	private void initImagePopWindow(int badgeImageId, String titleText,
			String contentText) {
		Log.d("MyTag", "initImagePopWindow");
		mImgView = getLayoutInflater().inflate(R.layout.badge_pop, null);
		mImgPop = new PopupWindow(mImgView, LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT, true);

		badgeBgLinearLayout = (RelativeLayout) mImgView
				.findViewById(R.id.BadgeBgLinearLayout);
		badgeBgLinearLayout.setOnClickListener(this);
		ImageView badgeImageView = (ImageView) mImgView
				.findViewById(R.id.PopBadgeRookieImageView);
		TextView badgeTitleTextView = (TextView) mImgView
				.findViewById(R.id.PopBadgeTitleTextView);
		TextView badgeContentTextView = (TextView) mImgView
				.findViewById(R.id.PopBadgeContentTextView);
		badgeImageView.setImageResource(badgeImageId);
		badgeTitleTextView.setText(titleText);
		badgeContentTextView.setText(contentText);

		mImgPop.setAnimationStyle(R.style.AnimationPreview);

		// 加上下面两行可以用back键关闭popupwindow，否则必须调用dismiss();
		ColorDrawable dw = new ColorDrawable(-00000);
		mImgPop.setBackgroundDrawable(dw);
		mImgPop.showAtLocation(badgeBgLinearLayout, Gravity.CENTER, 0, 0);
		// mImgPop.update(0, 0, 240, 340);
		mImgPop.update();

	}

	OnClickListener closeListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (mImgPop != null && mImgPop.isShowing()) {
				mImgPop.dismiss();
			}
		}
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (mImgPop != null && mImgPop.isShowing()) {
			mImgPop.dismiss();
		}
	}

}